package cn.zhx.xa.utils;



import cn.zhx.xa.model.vo.common.ResultCodeEnum;
import org.springframework.util.DigestUtils;

import java.nio.charset.StandardCharsets;

public class PasswordEncoder {
    private static final String SALT = "zhx";

    public static String encode(String password) {
        // 加密
        return SALT + "@" + DigestUtils.md5DigestAsHex((password + SALT).getBytes(StandardCharsets.UTF_8));
    }

    public static Boolean matches(String encodedPassword, String rawPassword) {
        if (encodedPassword == null || rawPassword == null) {
            return false;
        }
        if(!encodedPassword.contains("@")){
            throw new RuntimeException(String.valueOf(ResultCodeEnum.DATA_ERROR));
        }
        return encodedPassword.equals(encode(rawPassword));
    }
}
